// 检测网络状态的钩子
import { ref } from 'vue';
import { useMessage } from '../useMessage';

const networkState = ref(navigator.onLine);

const handleOnline = () => {
  if (!networkState.value) {
    useMessage({
      type: 'success',
      message: '系统网络,连接已恢复',
    });
  }
  networkState.value = true;
};

const handleOffline = () => {
  useMessage({
    type: 'error',
    message: '系统网络,连接已断开!',
  });
  networkState.value = false;
};

window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
export const useNetworkState = () => {
  return networkState;
};
